﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;

/// <summary>
/// 多图像跟踪脚本，挂载到 AR Session Origin 上，在 OnTrackImagesChanged() 中捕捉图像 added 操作，更改 Tracked Image Prefab 为下一个 Prefab，从而达到动态调整 Prefabd 的目的
/// 需要先把 AR Tracked Image Manager 的 Tracked Image Prefab 设置为空
/// </summary>
public class MultiImageTracking : MonoBehaviour
{
    GestureController mGestureController;
    ARTrackedImageManager mImgTrackedManager;
    //public GameObject[] mObjectPrefabs;
    private Dictionary<string, GameObject> mPrefabsDict = new Dictionary<string, GameObject>();

    private void Awake()
    {
        mImgTrackedManager = GetComponent<ARTrackedImageManager>();
        mGestureController = GetComponent<GestureController>();
        LogUtils.debug("MultiImageTracking mGestureController = " + mGestureController);
    }

    // Start is called before the first frame update
    void Start()
    {
        mPrefabsDict.Add("earth", Resources.Load("Prefabs/Astronaut") as GameObject);
        mPrefabsDict.Add("fish", Resources.Load("Prefabs/Drone") as GameObject);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnEnable()
    {
        mImgTrackedManager.trackedImagesChanged += OnTrackedImagesChanged;
    }

    private void OnDisable()
    {
        mImgTrackedManager.trackedImagesChanged -= OnTrackedImagesChanged;
    }

    void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    {
        foreach (var trackedImage in eventArgs.added)
        {
            //方式一
            //OnImagesChanged(trackedImage.referenceImage.name);

            //方式二/三
            OnImagesChanged(trackedImage);
        }

        //foreach (var trackedImage in eventArgs.updated)
        //{
        //    OnImagesChanged(trackedImage.referenceImage.name);
        //}
    }

    //方式一
    //
    //private void OnImagesChanged(string referenceImageName)
    //{
    //    if(referenceImageName == "earth")
    //    {
    //        mImgTrackedManager.trackedImagePrefab = mObjectPrefabs[1];
    //        LogUtils.debug("Tracked Name is " + referenceImageName);
    //        LogUtils.debug("Prefab Name is " + mImgTrackedManager.trackedImagePrefab.name);
    //    }
    //    if(referenceImageName == "fish")
    //    {
    //        mImgTrackedManager.trackedImagePrefab = mObjectPrefabs[0];
    //    }
    //}

    //方式二
    ///
    /// <summary>
    /// 自己负责 Prefab 的实例化，但是因为要每次都 if else 判断，所以改用方式三
    /// </summary>
    /// <param name="arTrackedImage"></param>
    /// 
    //private void OnImagesChanged(ARTrackedImage arTrackedImage)
    //{
    //    if(arTrackedImage.referenceImage.name == "earth")
    //    {
    //        Instantiate(mObjectPrefabs[0], arTrackedImage.transform);
    //    }
    //    if(arTrackedImage.referenceImage.name == "fish")
    //    {
    //        Instantiate(mObjectPrefabs[1], arTrackedImage.transform);
    //    }
    //}


    private void OnImagesChanged(ARTrackedImage arTrackedImage)
    {
        LogUtils.debug("OnImagesChanged image name=" + arTrackedImage.referenceImage.name);
        Instantiate(mPrefabsDict[arTrackedImage.referenceImage.name], arTrackedImage.transform);

        //调用 GestureController 实现手势操作
        //var spawnPrefab = mPrefabsDict[arTrackedImage.referenceImage.name];

        //LogUtils.debug("OnImagesChanged spawnPrefab= " + spawnPrefab);
        //var spawnedObject = Instantiate(spawnPrefab, arTrackedImage.transform);

        //LogUtils.debug("mGestureController.manipulator= " + mGestureController.manipulator);
        //var maniObject = Instantiate(mGestureController.manipulator, arTrackedImage.transform);

        //mGestureController.addPrefab(spawnedObject, maniObject, gameObject);
    }
}
